package edu.xlh.travel.GOGOZhiFu;

import edu.xlh.travel.pojo.User;
import edu.xlh.travel.service.UserService;
import edu.xlh.travel.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/gogo")
public class GOGOZhiFuTestController {
    @Autowired
    private IdWorker idWorker;
    private final static String APP_ID = "8664253cac7b5f41";
    private final static String APP_SECRET = "8664253cac7b5f41757eaf0ccc76d60e";
    @Autowired
    private UserService userService;

    @RequestMapping("/zhifu")
    //@ResponseBody
    public void zhiFu(HttpSession session , HttpServletResponse response) throws IOException {
        GoGoZhiFu goGoZhiFu = new GoGoZhiFu();
        PayParams payParams = new PayParams();
        payParams.setPrice("0.01");
        payParams.setType(1);
        payParams.setIsHtml(1);
        payParams.setPayId((idWorker.nextId()+"").substring(0 , 10));
        payParams.setSign("");
        payParams.setNotifyUrl("http://localhost:8080/gogo/notifyUrl");
        payParams.setReturnUrl("http://localhost:8080/dist/gogozhifuSusseccPage");
        payParams.setParam("");
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        goGoZhiFu.pay(user.getId(), payParams , response);
    }

    /*@PostMapping("/notifyUrl")
    public String notifyUrl(@RequestBody NotifyParams notifyParams, HttpServletResponse response) {
        GoGoZhiFu goGoZhiFu = new GoGoZhiFu();
        return goGoZhiFu.notify(notifyParams, response);
    }*/
    // 支付成功后，GOGO支付会通知开发者设置的notifyUrl
    //建议的controller写法如下：
    @PostMapping("/notifyUrl")
    @ResponseBody
    public String notifyUrl(@RequestBody NotifyParams notifyParams, HttpServletResponse response) {
        return notify(notifyParams, response);
    }
    // 接收GOGO支付完成的回调通知方法，在该函数中主要用于商户自己根据支付完成处理相应的数据逻辑
    public  String notify(NotifyParams notifyParams, HttpServletResponse response) {
        if (null != notifyParams) {
            String sign = GoGoZhiFu.generateNotifySign(notifyParams, APP_ID, APP_SECRET);
            if (sign.equals(notifyParams.getSign())) {
                // 在这里根据商户自己需求完成后续逻辑操作
                // 订单数据更新操作...
                String userId = GoGoZhiFu.userIdRelatePayIdContextMap.get(notifyParams.getPayId());
                userService.updateIsVipByUserId(userId , "1");
                // 成功完成后正确返回
                response.setStatus(HttpServletResponse.SC_OK);
                return "success";
            }
        }
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return "error";
    }

}
